import 'dart:developer';
import 'package:get/get.dart';
import 'package:ball_lion/entity/pay/recharge_entity.dart';
import 'package:ball_lion/entity/match/secret_price_entity.dart';
import 'package:ball_lion/server/api/coupon_api.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/server/api/pay_api.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:ball_lion/module/pay/pay_bottom_sheet.dart';

class InformationPayLogic extends GetxController {
  int _priceIndex = 0;
  int? roomId = Get.parameters['roomId'].toInt();
  String? tip;
  SecretPayEntity? data;
  List<SecretPrices>? price;
  int get priceIndex => _priceIndex;

  set priceIndex(int value) {
    _priceIndex = value;
    update();
  }

  @override
  void onReady() {
    requestData();
    super.onReady();
  }

  Future toPay(SecretPrices price) async {
    Utils.onEvent("hwqb_ljzf");
    double money = price.price ?? 0;
    final coupons = await CouponApi.use1Coupons(
        gold: money, scene: PayScene.secret, otherId: '', roomId: roomId);
    final PayResult? result = await Get.bottomSheet(
        isScrollControlled: true,
        PayBottomSheet(
            payInfo: PayInfo(
                price: money.toString(),
                payScene: PayScene.secret,
                buyId: price.id.toString(),
                coupons: coupons)));
    if (result != null && result.toPay == PayStatus.goldPay) {
      var s = await PayApi.secretOrder(price.id,
          couponGold: result.coupon?.gold,
          couponId: result.coupon?.id,
          roomId: roomId);
      if (s == true) {
        Utils.onEvent('pay_done');
        Get.back();
      } else {
        Utils.onEvent('jmnc_zfyd_qxzf');
      }
    } else if (result?.toPay == PayStatus.directPay) {
      Utils.onEvent('jmnc_zfyd_qrzf');
      Get.back();
    } else {
      Utils.onEvent('jmnc_zfyd_qxzf');
    }
    User.fetchUserInfo(fetchFocus: false);
  }

  Future requestData() async {
    data = await PayApi.getSecretPay(roomId: roomId);
    price = data?.priceList;
    var tips = await OtherApi.getAppList("app_res_tips");
    try {
      tip = tips
          ?.firstWhere((element) => element.button == "secret_pay_tip")
          .content;
      update();
    } catch (e) {
      log("no secret_pay");
    }
    update();
  }
}
